package org.hcyspartnoc.luoxin.controller.system;

import cn.hutool.core.bean.BeanUtil;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.system.SysRoleException;
import org.hcyspartnoc.luoxin.pojo.dto.system.SysRoleAddDto;
import org.hcyspartnoc.luoxin.pojo.dto.system.SysRoleModifyDto;
import org.hcyspartnoc.luoxin.pojo.po.system.SysRolePo;
import org.hcyspartnoc.luoxin.pojo.vo.system.SysRoleVo;
import org.hcyspartnoc.luoxin.service.system.ISysRoleService;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;

    @Log(title = "系统·角色·新增", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "system:role:add", descr = "新增系统角色")
    @PostMapping("/add")
    public AjaxResult add(@Valid @RequestBody SysRoleAddDto sysRoleAddDto) throws SysRoleException {
        SysRoleVo sysRoleVo = sysRoleService.addRole(sysRoleAddDto);
        return AjaxResult.success(sysRoleVo);
    }

    @Log(title = "系统·角色·修改", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "system:role:modify", descr = "修改系统角色")
    @PutMapping("/modify")
    public AjaxResult modify(@Valid @RequestBody SysRoleModifyDto sysRoleModifyDto) throws SysRoleException {
        SysRoleVo sysRoleVo = sysRoleService.modifyRole(sysRoleModifyDto);
        return AjaxResult.success(sysRoleVo);
    }

    @Log(title = "系统·角色·删除·通过id", businessType = BusinessType.DELETE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:remove:id", descr = "删除系统角色")
    @DeleteMapping("/remove/{roleId:\\d+}")
    public AjaxResult remove(@PathVariable Integer roleId) throws SysRoleException {
        sysRoleService.removeRole(roleId);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·角色·查询·分页", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:query:page", descr = "分页查询系统角色")
    @GetMapping("/query/page")
    public AjaxResult queryByPage(@Min(value = 1, message = "当前页不能小于1")
                                  @RequestParam(defaultValue = "1") int currentPage,
                                  @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  @Length(max = 10, message = "名称长度0-10")
                                  @RequestParam(defaultValue = "") String name,
                                  @Min(value = 0, message = "状态值0-2")
                                  @Max(value = 2, message = "状态值0-2")
                                  @RequestParam(defaultValue = "0") Integer status) {
        return AjaxResult.success(sysRoleService.queryByPage(currentPage, pageSize, name, status));
    }

    @Log(title = "系统·角色·查询·所有", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:query:all", descr = "查询所有角色")
    @GetMapping("/query/all")
    public AjaxResult queryAll() {
        List<SysRolePo> list = sysRoleService.list();
        ArrayList<SysRoleVo> sysRoleVos = new ArrayList<>();
        for (SysRolePo sysRolePo : list) {
            sysRoleVos.add(BeanUtil.copyProperties(sysRolePo, SysRoleVo.class));
        }
        return AjaxResult.success(sysRoleVos);
    }

    @Log(title = "系统·角色·修改·状态", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:modify:status", descr = "修改角色状态")
    @PutMapping("/modify/status/{roleId}")
    public AjaxResult modifyStatus(@PathVariable Integer roleId) throws SysRoleException {
        sysRoleService.modifyStatus(roleId);
        return AjaxResult.successMsg();
    }
}
